<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
class Product_model extends CI_Model {
	public function __construct() {
		parent::__construct ();
	}
	/*
	 * 返回商品的数量
	 */
	public function count_products($category,$shelve,$name){
		if($category!=0){
			$this->db->where('categoryid',$category);
		}
		if($shelve!=-1){
			$this->db->where('shelve',$shelve);
		}
		if(!empty($name)){
			$this->db->like('t_product.name', $name);
		}
		$this->db->where('defaults=0');
		$query = $this->db->get('t_product');
		return $query->num_rows();
	}
	/*
	 * 根据分页信息得到商品信息
	 */
	public function get_products($category,$shelve,$name,$page,$offset){
		$this->db->select('t_product.id,t_category.name as category,t_product.name,t_product.description,t_product.price,t_product.unit,t_product.week,t_product.shelve');
		$this->db->from('t_product');
		$this->db->join('t_category', 't_product.categoryid = t_category.id','left');
		if($category!=0){
			$this->db->where('categoryid',$category);
		}
		if($shelve!=-1){
			$this->db->where('shelve',$shelve);
		}
		if(!empty($name)){
			$this->db->like('t_product.name', $name);
		}
		$this->db->where('defaults=0');
		$this->db->limit($page, $offset);
		$this->db->order_by("t_product.id", "desc");
		$query = $this->db->get();
		return $query->result_array();
	}
	/*
	 * 根据商品ID得到商品信息
	 */
	public function load($id){
		$query = $this->db->get_where('t_product',array('id' => $id));
		if ($row = $query->row_array()){
			return $row;
		}
		return array();
	}
	/*
	 * 添加商品信息
	 */
	public function create($product){
		$this->db->insert('t_product', $product);
		return $this->db->insert_id();
	}
	/*
	 * 更新商品信息
	 */
	public function update($id,$product){
		$this->db->where('id', $id);
		$this->db->update('t_product', $product);
		return $this->db->affected_rows()>0;
	}
	/*
	 * 删除商品信息
	 */
	public function delete($id){
		$this->db->where('id', $id);
		$this->db->delete('t_product');
		return $this->db->affected_rows()>0;
	}
	/*
	 * 根据分类ID得到商品信息
	 */
	public function find_by_cat($categoryid){
		$this->db->where('categoryid',$categoryid);
		$query = $this->db->get('t_product');
		return $query->result_array();
	}
	/*
	 * 根据日期得到商品信息(暂时没用)
	 */
	public function find_by_week($week,$schoolid){
		$this->db->select('t_product.id,t_product.price');
		$this->db->from('t_product');
		$this->db->join('t_connect', 't_connect.productid = t_product.id','left');
		$this->db->where('t_connect.week',$week);
		$this->db->where('schoolid',$schoolid);
		$this->db->where('t_connect.select',1);
		$query = $this->db->get();
		return $query->row_array();
	}
	/*
	 * 返回所有上架商品信息(暂时没用)
	 */
	public function get_all_products(){
		$this->db->select('t_product.id,t_product.name');
		$this->db->from('t_product');
		$this->db->where('shelve=0');
		$query = $this->db->get();
		return $query->result_array();
	}
	/*
	 * 根据商品名称返回商品列表
	 */
	public function find_by_name($name,$page,$offset){
		if(!empty($name)){
			$this->db->like('name', $name);
		}
		$this->db->where('defaults=0');
		$this->db->limit($page, $offset);
		$query=$this->db->get('t_product');
		return $query->result_array();
	}
	/*
	 * 根据商品名称返回商品数量
	 */
	public function count_by_name($name){
		if(!empty($name)){
			$this->db->like('name', $name);
		}
		$this->db->where('defaults=0');
		$num = $this->db->count_all_results('t_product');
		return $num;
	}
	/*
	 * 根据分类ID得到商品信息(用于获取默认餐)
	 */
	public function find_by_category($categoryid){
		$this->db->where('categoryid',$categoryid);
		$query = $this->db->get('t_product');
		return $query->row_array();
	}
}
